package com.hand.wfive.util

import android.content.Context
import com.hand.wfive.App
import com.hand.wfive.BuildConfig
import com.hand.wfive.R
import com.hand.wfive.entity.AppUpdateModel
import com.hand.wfive.entity.AppUpdateResp
import com.hand.wfive.loginAndVip.ApiConfig
import com.hand.wfive.loginAndVip.LoginConfig
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import model.UpdateConfig
import rxhttp.wrapper.param.RxHttp
import update.UpdateAppUtils

object CheckVersionUtils {

    fun checkAppVersion(event: (Int) -> Unit ={}) {
        val disposable = RxHttp.postForm(ApiConfig.appIsRenew)
            .add("appid", LoginConfig.AppId)
            .add("versionCode", BuildConfig.VERSION_CODE)
            .add("channel", App.getContext().getString(R.string.channel))
            .asClass(AppUpdateResp::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                event(it.code)
                if (it.code == ApiConfig.SUCCESSCODE) {
                    val data = it.obj
                    showUpgradeDialog(data)
                }
            }
    }

    private fun showUpgradeDialog(appUpdateModel: AppUpdateModel) {
        // 更新配置
        val updateConfig = UpdateConfig().apply {
            force = appUpdateModel.isRenew == 1
            checkWifi = true
//            needCheckMd5 = true
//            isShowNotification = true
//            notifyImgRes = R.drawable.ic_logo
//            apkSavePath = Environment.getExternalStorageDirectory().absolutePath +"/teprinciple"
//            apkSaveName = "teprinciple"
        }
        UpdateAppUtils
            .getInstance()
            .apkUrl(appUpdateModel.apkUrl)
            .updateTitle("版本更新")
            .updateConfig(updateConfig)
            .updateContent("更新版本：${appUpdateModel.versionName}\n${appUpdateModel.renewContent}")
            .update()
    }
}